﻿using ToolKitIESiteList.Library;
using ToolKitManagerIESiteList.Util;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics.CodeAnalysis;

namespace ToolKitManagerIESiteList
{
    public partial class FrmCopyDomain : Form
    {
        [SuppressMessage("Microsoft.Performance", "CA1819")]
        public string[] DataCopyDomains { get; set; }
        public string PublicDomain { get; private set; }
        public string LocalDomain { get; private set; }
        public bool RemovePublicPort { get; private set; }

        public FrmCopyDomain()
        {
            InitializeComponent();
        }

        private void FrmCopyDomain_Load(object sender, EventArgs e)
        {
            CboDomains.Items.Clear();
            foreach (var item in DataCopyDomains.OrderBy(d => d.ToLower()))
            {
                CboDomains.Items.Add(item);
            }
        }

        private void BntCopy_Click(object sender, EventArgs e)
        {
            if (CboDomains.SelectedItem == null)
            {
                this.ShowWarning(Properties.Resources.SelectOneDomain, AssemblyHelper.AssemblyTitle);
                CboDomains.Focus();
                return;
            }
            if (TxtPublicDomain.Text.Trim().Length == 0)
            {
                this.ShowWarning(Properties.Resources.PublicDomainRequeried, AssemblyHelper.AssemblyTitle);
                TxtPublicDomain.Focus();
                return;
            }
            var newtext = HelperEmie.CreateValidUri(TxtPublicDomain.Text.Trim());
            if (newtext == null)
            {
                this.ShowWarning(Properties.Resources.UrlNotValid, AssemblyHelper.AssemblyTitle);
                TxtPublicDomain.Focus();
                return;
            }
            if (EmieDocument.GetDomains(TxtPublicDomain.Text.Trim(), ChkRemovePort.Checked).Length > 0)
            {
                this.ShowWarning(Properties.Resources.DomainAlreadyExists, AssemblyHelper.AssemblyTitle);
                TxtPublicDomain.Focus();
                return;
            }
            LocalDomain = CboDomains.SelectedItem.ToString();
            PublicDomain = TxtPublicDomain.Text.Trim();
            RemovePublicPort = ChkRemovePort.Checked;
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void BntClose_Click(object sender, EventArgs e)
        {
            LocalDomain = string.Empty;
            PublicDomain = string.Empty;
            RemovePublicPort = false;
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void CboDomains_SelectionChangeCommitted(object sender, EventArgs e)
        {
            TxtPublicDomain.Focus();
        }
    }
}
